package Vocabulary.Test;

import Vocabulary.Mapper.WordMapper;
import Vocabulary.MybatisUtils;
import Vocabulary.Word;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.lang.Thread.sleep;
public class Crawler {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
    public static void main(String[] args) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
        String mp3Url = "https://res.iciba.com/resource/amp3/oxford/0/f8/1b/f81bbe4257062153bec6cd6df0f06dfb.mp3";
        try {
            Connection.Response resultImageResponse = Jsoup.connect(mp3Url).ignoreContentType(true).execute();
            InputStream in = resultImageResponse.bodyStream();
            OutputStream out = new FileOutputStream("src//main//java//output.mp3");
            byte[] buffer = new byte[4096];
            int n;
            while ((n = in.read(buffer)) > -1) {
                out.write(buffer, 0, n);
            }
            out.close();
            in.close();
            System.out.println("Mp3 file saved!");
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
    @Test
    public void Mp3Crawler() throws Exception {

        int i1=0;
        for (int i=0;i<=225;i++){
            Document parse = Jsoup.parse(new URL("https://word.iciba.com/?action=words&class=11&course="+i), 6000);
            Elements wordMainList = parse.getElementsByClass("word_main_list");

            for (Element element:wordMainList){
                Elements children = element.children();
                for (Element element1:children){
                    i1++;
                    System.out.println(i1);
                    Elements children1 = element1.children();
                    String id = children1.get(1).children().get(1).attr("id");

                    for (Element element2:children1){
                        int o=0;
                        List<String> strings = element2.getElementsByTag("span").eachText();
                        if (strings.size()>0){
//                            System.out.println(strings.get(0)+"\n\n\n\n");
                            if (o==0){
                                try {
                                    Connection.Response resultImageResponse = Jsoup.connect(id).ignoreContentType(true).execute();
                                    InputStream in = resultImageResponse.bodyStream();
//                                    OutputStream out = new FileOutputStream("src\\main\\java\\Vocabulary\\mp3\\"+strings.get(0)+".mp3");
                                    byte[] buffer = new byte[4096];
                                    byte[] bytes = in.readAllBytes();
                                    int n;
//                                    while ((n = in.read(buffer)) > -1) {
//                                        out.write(buffer, 0, n);
//                                    }
                                    wordMapper.updat(strings.get(0),bytes);
//                                    out.close();
                                    in.close();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                        o++;
                    }
                }
            }
            System.out.println(i);
        }
    }
    public static void clearFolder(String folderPath) {
        File folder = new File(folderPath);
        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        clearFolder(file.getAbsolutePath()); // �ݹ�������ļ���
                    } else {
                        file.delete(); // ɾ���ļ�
                    }
                }
            }
            System.out.println("Folder contents have been cleared.");
        } else {
            System.out.println("Folder does not exist or is not a directory.");
        }
    }
    @Test
    public void T(){
        clearFolder("src\\main\\java\\Vocabulary\\mp3");
    }
    public void T1(){
        String mp3Url = "https://res.iciba.com/resource/amp3/oxford/0/f8/1b/f81bbe4257062153bec6cd6df0f06dfb.mp3";
        try {
            Connection.Response resultImageResponse = Jsoup.connect(mp3Url).ignoreContentType(true).execute();
            InputStream in = resultImageResponse.bodyStream();
            OutputStream out = new FileOutputStream("output.mp3");
            byte[] buffer = new byte[4096];
            int n;
            while ((n = in.read(buffer)) > -1) {
                out.write(buffer, 0, n);
            }
            out.close();
            in.close();
            System.out.println("Mp3 file saved!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void clear(){
        wordMapper.becometozero();
    }
    @Test
    public void CrawlerWords() throws Exception {
        for (int i=0;i<=225;i++){
            Document parse = Jsoup.parse(new URL("https://word.iciba.com/?action=words&class=11&course="+i), 6000);
            sleep(5000);
            Elements wordMainList = parse.getElementsByClass("word_main_list");
            for (Element element:wordMainList){
                Elements children = element.children();
                for (Element element1:children){
                    BufferedWriter bw = null;
                    bw = new BufferedWriter(new FileWriter("src//Vocabulary/Vocabulary.txt", true));
                    Elements children1 = element1.children();
                    for (Element element2:children1){
                        List<String> strings = element2.getElementsByTag("span").eachText();
                        if (strings.size()>0){
                            bw.write(strings.get(0)+"                 ");
                        }
                    }
                    bw.newLine();
                    bw.close();
                }
            }
            System.out.println(i);
        }
    }
    @Test
    public void insert(){
        String fileName = "F:\\Desktop\\workspace\\crawler\\src\\main\\java\\Vocabulary\\Vocabulary.txt";
        List<List<String>> data = processFile(fileName);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        WordMapper wordMapper = sqlSession.getMapper(WordMapper.class);
        for (List<String> line : data) {
//            System.out.println(line.get(1));
            System.out.println(line.size());
            Word word=new Word(line.get(0),line.get(1));
            wordMapper.insert(word);
        }
    }
    public static List<List<String>> processFile(String fileName) {
        List<List<String>> result = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] words = line.split("                 ");
                List<String> wordList = Arrays.asList(words);
                result.add(wordList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}